package Lambda;

import org.junit.Before;
import org.junit.Test;

import java.util.*;

public class Example1 {

    List<Employee> employees;

    @Before
    public void init(){
        employees=new ArrayList<>();
        employees.add(new Employee(1,"张三", 15,8000,"销售部"));
        employees.add(new Employee(2,"李四", 35,6000,"客服"));
        employees.add(new Employee(3,"王五", 25,10000,"财务部"));
        employees.add(new Employee(4,"赵六", 35,11000,"财务部"));
        employees.add(new Employee(5,"田七", 22,12000,"管理部"));
        employees.add(new Employee(6,"老八", 26,15000,"技术部"));
        employees.add(new Employee(7,"三炮", 27,9000,"技术部"));
        employees.add(new Employee(8,"李九", 29,11000,"管理部"));
        employees.add(new Employee(9,"周十", 48,18000,"技术部"));
    }

    @Test
    public void test1(){
        Collections.sort(employees, Comparator.comparingInt(Employee::getAge));
        employees.forEach(System.out::println);
    }

    @Test
    public void test2(){
        System.out.println(handleString("hello",str -> str.toUpperCase()));
        System.out.println(handleString("hello",str -> str.substring(0,3)));
    }

    public String handleString(String str,MyStringHandler handler){
        return handler.handle(str);
    }

    @FunctionalInterface
    interface MyStringHandler {
        String handle(String str);
    }


    @Test
    public void test3(){
        System.out.println(handleLong(1500,2500, (a,b)-> a+b));
        System.out.println(handleLong(1500,2500, (a,b)-> a*b));

    }

    public long handleLong(long a,long b,LongHandler<Long,Long> handler){
        return handler.handleFunction(a,b);
    }

    @FunctionalInterface
    interface LongHandler<T,R>{
        R handleFunction(T x,T y);
    }

    @Test
    public void test4(){
        String [] keys = {"a","a","a","a"};
        for (String k : keys) {
            int hasCode = k.hashCode();
            int right = hasCode >>> 16; //无符号右移16位
            int hash = hasCode ^ right;  //做异或运算。
            int i = (16 - 1) & hash; // 截掉了高位，只取低4位，它表示的范围就是在16以内。
            System.out.println(hash + " 下标：" + i);
        }
    }

}